<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				listCacheKey:"mkt-tj-productBargain-list-cache"
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			formSubmit(){
				let _that = this
				if(_that.edit){
					_that.om.formSave("productBargain/doUpdate",_that.formdata,_that)
				}else{
					_that.om.formSave("productBargain/doAdd",_that.formdata,_that)
				}
				
			},
			initLoad(options){
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					urlUtil.sysdictComboboxUrl("38525"),
					urlUtil.sysdictComboboxUrl("38526")
				]
				if(fileId){
					ajaxArr.push({
						url:"productBargain/getValue",
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					changeEditor("title1","userPostId",{
						link:"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify({
								firstUserId: _that.userId
							}),
							initLoad:false,
							url:"orgInstPosUserOfficial/findListByOrg",
							namefield:"userPosName",
							cachename:_that.listCacheKey,
							searchfield:"q",
							placeholder:"请输入姓名或岗位"
						}
					},null,mainData)
					changeEditor("title2","orderTypeId","data",configSelectData(res[1],"id","dicname"),mainData)
					changeEditor("title2","saleTypeId","data",configSelectData(res[2],"id","dicname"),mainData)
					changeEditor("title2","reducedRate",{
						isShowTips:true,
						tips:"计算公式:(指导价金额合计—特价金额合计)/指导价金额合计"
					},null,mainData)
					if(_that.subEditorObj["title3"]){
						changeEditor("title3","productStr",{
							titleTemplate:"产品编码:<%=projectCode || '未填写'%>",
							subtitle1Template:"产品名称:<%= projectName || '未填写' %>",
							subtitle2Template:"市场指导价<%= marketPrice || '未填写' %>|指导价金额:<%=marketTotalPrice||'未填写'%>",
							subtitle3Template:"最低售价:<%=lowestPrice||'未填写'%>|申请特价:<%=applyPrice || '未填写'%>|特价金额:<%=applyTotalPrice||'未填写'%>"
						},null,mainData)
					}
					
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[3].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[3].value[index])
								_that.$set(_that.formdata,index,res[3].value[index])
							}
						}
						let dataList = _that._.clone(res[3].value.productList)
						_that.subEditorObj["title3"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"productStr",JSON.stringify(dataList))
						_that.$set(_that.formdata,"productStr",JSON.stringify(dataList))
						delete res[3].value.dataList
						
						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex("title2","saleTypeId")
							_that.$refs.autoform.initSelectIndex("title2","orderTypeId")
						})
					}
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onUnload(){
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for (var fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.listCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userPostId = d.id
							formdata.userId = d.userId
							formdata.userName = d.userPosName
							formdata.postId = d.posId
							formdata.postName = d.posName
							formdata.instName = d.instName
							formdata.instId = d.instId
							formdata.mobile = d.mobile
							uni.removeStorage({
								key:_that.listCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
